<?php
declare (strict_types=1);

namespace app\common\queue\split;

use app\common\exception\LogicException;
use app\user\logic\user\{Income, IncomeFlow, Split, SplitCourse, SplitFlow};
use think\facade\Db;

/**
 * 用户分成收入解冻类
 */
class Unfrozen
{
    public function run()
    {
        // 退款配置
        // $refundConfig = SystemConfig::get('refund.config', ['effective_days' => 0]);
        // $refundDays   = $refundConfig['effective_days'];
        $refundDays = 7;
        $time       = time();
        $ufTime     = $time - $refundDays * 86400;
        $map        = '(status = 0 AND unfrozen_time is NULL AND create_time < ' . $ufTime . ') OR (status = 0 AND unfrozen_time is not NULL AND unfrozen_time < ' . $time . ')';
        $data       = app(SplitCourse::class)->whereRaw($map)->select();

        if (!$data->isEmpty()) {
            foreach ($data as $v) {
                try {
                    Db::startTrans();
                    $v->status = 1;
                    if (!$v->save()) throw new LogicException('split update error');

                    app(SplitFlow::class)->where('rel_id', $v->id)->where('create_time', '<', $time)->where('status', 0)->select()->each(function ($item) {
                        // 解冻
                        $user_split         = app(Split::class)->where('user_id', $item->user_id)->findOrEmpty();
                        $user_split->amount += $item->num;
                        $user_split->frozen -= $item->num;
                        if (!$user_split->save()) throw new LogicException('用户分成解冻失败');
                        // 流水状态
                        $item->status = 1;
                        if (!$item->save()) throw new LogicException('用户分成流水状态修改失败');
                        // 增加收入
                        $user_income = app(Income::class)->where('user_id', $item->user_id)->findOrEmpty();
                        if ($user_income->isEmpty()) {
                            // 新增
                            $add = Income::create([
                                'user_id'      => $item->user_id,
                                'income'       => $item->num,
                                'total_income' => $item->num,
                            ]);
                            if ($add->isEmpty()) throw new LogicException('用户收入新增失败');

                            $_income = $item->num;
                        } else {
                            // 变更
                            $user_income->income       += $item->num;
                            $user_income->total_income += $item->num;
                            if (!$user_income->save()) throw new LogicException('用户收入变更失败');

                            $_income = $user_income->income;
                        }
                        // 流水记录
                        app(IncomeFlow::class)->updateFlow($item->user_id, $item->type, $item->num, $_income, 'user_split_flow', $item->id, $item->note . '：' . $item->num);
                    });
                    Db::commit();
                } catch (LogicException $e) {
                    Db::rollback();
                    throw new LogicException($e->getMessage());
                }
            }
        }
    }
}
